// app/controller/search.js
const Controller = require('egg').Controller;

class SearchController extends Controller {
    async globalSearch() {
        const { ctx } = this;
        const { keyword, type = 'all', page = 1, pageSize = 10 } = ctx.query;

        if (!keyword || keyword.trim().length === 0) {
            ctx.body = { code: 400, message: '搜索关键词不能为空' };
            return;
        }

        try {
            const searchTypes = ['user', 'circle', 'post', 'job', 'good', 'lostfound'];
            let results = {};

            if (type === 'all') {
                // 并行搜索所有类型
                const promises = searchTypes.map(searchType =>
                    this.searchByType(keyword, searchType, 1, 5) // 全部搜索只取前5条
                );
                const allResults = await Promise.all(promises);

                // 构建结果对象
                searchTypes.forEach((searchType, index) => {
                    results[searchType] = allResults[index];
                });
            } else {
                // 搜索特定类型
                results = await this.searchByType(keyword, type, page, pageSize);
            }

            ctx.body = { code: 0, data: results };
        } catch (error) {
            console.error('全局搜索错误:', error);
            ctx.body = { code: 500, message: '搜索失败' };
        }
    }

    async searchByType(keyword, type, page, pageSize) {
        const { ctx } = this;

        try {
            switch (type) {
                case 'user':
                    return await ctx.service.user.getUsers({
                        keyword,
                        page,
                        pageSize
                    });

                case 'circle':
                    return await ctx.service.circle.getCircles({
                        keyword,
                        page,
                        pageSize
                    });

                case 'post':
                    return await ctx.service.post.getPosts({
                        search_keyword: keyword,
                        search_type: 'post',
                        page,
                        pageSize
                    });

                case 'job':
                    return await ctx.service.job.list({
                        keyword,
                        page,
                        pageSize
                    });

                case 'good':
                    return await ctx.service.good.getGoodsSimple({
                        keyword,
                        page,
                        pageSize
                    });

                case 'lostfound':
                    return await ctx.service.lostAndFound.getItemsSimple({
                        title: keyword,
                        page,
                        pageSize
                    });

                default:
                    return { list: [], pagination: { total: 0 } };
            }
        } catch (error) {
            console.error(`${type}搜索错误:`, error);
            return { list: [], pagination: { total: 0 } };
        }
    }
}

module.exports = SearchController;